package com.vx.vxbug.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vx.vxbug.entities.Program;

@Repository("programDao")
public class SqlProgramDao implements IProgramDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional(propagation = Propagation.REQUIRED)
	public void addProgram(Program b) {
		entityManager.persist(b);

	}

	public SqlProgramDao() {
		super();
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Program getProgramById(int id) {
		return entityManager.find(Program.class, id);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Program> getAllPrograms() {
		List<Program> result = entityManager.createQuery("FROM Program").getResultList();
		return result;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void updateProgram(Program b) {
		entityManager.refresh(b);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteProgram(Program b) {
		entityManager.remove(b);
	}

}
